package com.haofengsoft.scce;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.haofengsoft.scce.common.config.CommonConfig;
import com.haofengsoft.scce.db.DaoMaster;
import com.haofengsoft.scce.db.DaoSession;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class ManageApplication extends Application {

    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private List<Activity> activityList = new LinkedList<Activity>();
    public static Context CONTEXT = null;

    // 表示当前是否有网络连接, 默认为false
    private static boolean isNetConnected = false;

    // 表示当前是否是wifi状态下,默认false;
    private static boolean isWifiConnected = false;

    // 表示当前是否在移动网络下,默认false
    private static boolean isMobileConnected = false;

    private static ManageApplication instance;

    public static boolean isWifiConnected() {
        return isWifiConnected;
    }

    public static void setWifiConnected(boolean isWifiConnected) {
        ManageApplication.isWifiConnected = isWifiConnected;
    }

    public static boolean isMobileConnected() {
        return isMobileConnected;
    }

    public static void setMobileConnected(boolean isMobileConnected) {
        ManageApplication.isMobileConnected = isMobileConnected;
    }

    // set/get方法
    public static boolean isNetConnected() {
        return isNetConnected;
    }

    public static void setNetConnected(boolean isNetConnected) {
        ManageApplication.isNetConnected = isNetConnected;
    }

    // 单例模式获取唯一的MyApplication
    public static ManageApplication getInstance() {
        if (instance == null) {
            instance = new ManageApplication();
        }
        return instance;

    }

    public void onCreate() {
        super.onCreate();
        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder()// 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                        // .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);

        CONTEXT = this.getApplicationContext();
        // 创建项目文件
        // 初始化
        File projectFile = new File(CommonConfig.DATA_FILE_PATH);
        if (!projectFile.exists()) {
            projectFile.mkdirs();
        }
        File imageFile = new File(CommonConfig.PROJECT_IMAGE_PATH);
        if (!imageFile.exists()) {
            imageFile.mkdirs();
        }
        File videoFile = new File(CommonConfig.PROJECT_VIDEO_PATH);
        if (!videoFile.exists()) {
            videoFile.mkdirs();
        }
        File tempFile = new File(CommonConfig.PROJECT_TEMP_PATH);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        File cacheFile = new File(CommonConfig.PROJECT_CACHE_PATH);
        if (!cacheFile.exists()) {
            cacheFile.mkdirs();
        }
        File safetyFile = new File(CommonConfig.DATA_SAFETY_FILE);
        if (!safetyFile.exists()) {
            safetyFile.mkdirs();
        }
    }

    public static Context getContext() {
        return CONTEXT;
    }

    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    /**
     * 取得DaoSession
     *
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    /**
     * 取得DaoMaster
     *
     * @param context
     * @return
     */
    public static DaoMaster getDaoMaster(Context context) {
        if (daoMaster == null) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context,
                    CommonConfig.DATABASE_NAME, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }

}
